package com.loongtech.bi.entity.system;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.loongtech.core.util.Time;
import org.quartz.Trigger.TriggerState;
import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Entity
@Table(name = "bi_monitor_config")
@JsonIgnoreProperties({"jobTrigger"})
public class EntityMonitorConfig implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    private Integer id;

       // 查询语句
    @Column(columnDefinition = "varchar(255) NOT NULL default ''")
    private String inquerySQL;

    // 监控描述
    @Column(columnDefinition = "varchar(255) NOT NULL default ''")
    private String description;

    // 监控产出说明
    @Column(columnDefinition = "varchar(255) NOT NULL default ''")
    private String subDescription;

    // 监控组别
    @Column(columnDefinition = "int(11) NOT NULL default 1")
    private Integer groupId;

    // 告警阈值
    @Column(columnDefinition = "bigint(20) NOT NULL")
    private Long threshHold;

    // 触发器
    @Transient
    private String jobTrigger;

    // 任务状态 (NONE, NORMAL, PAUSED, COMPLETE, ERROR, BLOCKED)
    @Column
    private String status = TriggerState.NORMAL.toString();

    // 任务运行表达式
    // @Column(nullable = false)
    // private String cronExpression;

    // 任务创建时间
    @Column
    private long creatTime;

    // 任务开始时间
    @Column(nullable = false)
    private Date startTime;

    // 任务结束时间
    @Column(nullable = false)
    private Date endTime;

    // 监控频率
    @Column(columnDefinition = "int(11) NOT NULL default 0")
    private Integer intervalTime;

    // 监控次数
    @Column(columnDefinition = "int(11) NOT NULL default 0")
    private Integer countNum;

    @PrePersist
    public void createUpdate() {
        this.setCreatTime(System.currentTimeMillis());
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getSubDescription() {
        return subDescription;
    }

    public void setSubDescription(String subDescription) {
        this.subDescription = subDescription;
    }

    public String getDescription() {

        return description == null ? null : description.replaceAll("@thresh", threshHold + "");
    }

    public void setDescription(String description) {
        if (description.lastIndexOf("于") < 0) {
            this.description = description;
        } else {
            this.description = description.substring(0, description.lastIndexOf("于") + 1) + "@thresh";
        }
    }

    // 游戏名称
    @Transient
    private String gameName;
    // 游戏id
    @Column(columnDefinition = "varchar(255) default ''")
    private String gameId;

    // public Long getThreshHold() {
    // return threshHold;
    // }
    //
    // public void setThreshHold(Long threshHold) {
    // this.threshHold = threshHold;
    // }

    public String getJobTrigger() {
        return jobTrigger;
    }

    public void setJobTrigger(String jobTrigger) {
        this.jobTrigger = jobTrigger;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public Date getStartTime() {
        return startTime;
    }

    public String getInquerySQL() {
        return inquerySQL == null ? null : inquerySQL.replaceAll("@date", Time.getDateStr(Time.currentDate())).replaceAll("@thresh", threshHold + "");
    }

    public void setInquerySQL(String inquerySQL) {
        if (null == inquerySQL || inquerySQL.length() <=0) {
            this.inquerySQL = "";
        } else {
          /*  if (!inquerySQL.contains("@thresh") && inquerySQL.lastIndexOf(">") != -1) {
                inquerySQL = inquerySQL.substring(0, inquerySQL.lastIndexOf(">") + 1) + "@thresh";
            }*/
            Pattern pattern = Pattern.compile("dt='.*?'");
            Matcher matcher = pattern.matcher(inquerySQL);
            inquerySQL = matcher.replaceAll("dt='@date'");
            this.inquerySQL = inquerySQL;
        }
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public Date getEndTime() {
        return endTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    public Integer getIntervalTime() {
        return intervalTime;
    }

    public void setIntervalTime(Integer intervalTime) {
        this.intervalTime = intervalTime;
    }

    public Integer getCountNum() {
        return countNum;
    }

    public void setCountNum(Integer countNum) {
        this.countNum = countNum;
    }

    public long getCreatTime() {
        return creatTime;
    }

    public void setCreatTime(long creatTime) {
        this.creatTime = creatTime;
    }

    public Integer getGroupId() {
        return groupId;
    }

    public void setGroupId(Integer groupId) {
        this.groupId = groupId;
    }

    public Long getThreshHold() {
        return threshHold;
    }

    public void setThreshHold(Long threshHold) {
        this.threshHold = threshHold;
    }

    public String getGameName() {
        return gameName;
    }

    public void setGameName(String gameName) {
        this.gameName = gameName;
    }

    public String getGameId() {
        return gameId;
    }

    public void setGameId(String gameId) {
        this.gameId = gameId;
    }
}
